package com.wefans.wefansapp.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wefans.wefansapp.R;
import com.wefans.wefansapp.common.tool.data.BenefitResultData;
import com.wefans.wefansapp.common.tool.data.BenefitResultResponseData;
import com.wefans.wefansapp.common.tool.data.BenefitUserData;
import com.wefans.wefansapp.common.tool.data.BenefitUserResponseData;
import com.wefans.wefansapp.common.tool.data.BenefitUserResponseWinData;
import com.wefans.wefansapp.common.tool.data.BenefitVoteData;
import com.wefans.wefansapp.common.tool.data.MFDataManager;
import com.wefans.wefansapp.common.tool.debug.MFDebug;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager.IOnNetworkListener;
import com.wefans.wefansapp.common.tool.network.model.NetImage;
import com.wefans.wefansapp.common.tool.network.model.NetJsonData;
import com.wefans.wefansapp.common.tool.network.model.NetMessage;
import com.wefans.wefansapp.common.tool.preference.MFSharedPreference;

public class BenefitUserActivity extends BaseActivity implements IOnNetworkListener {

	private BenefitUserData m_UserData= new BenefitUserData();
	private ArrayList<BenefitUserResponseWinData> mBenefitUserItems =  new ArrayList<BenefitUserResponseWinData>();
	private ArrayList<BenefitUserResponseData> mBenefitUserData =  new ArrayList<BenefitUserResponseData>();
	private BenefitVoteData mBenefitVoteDate = new BenefitVoteData();
	private BenefitUserAdaptor mBenefitUserAdaptor = null;
	private ListView mLvBenefitResult = null;
    private DrawerLayout   m_DrawerLayout  = null;
    private FrameLayout    m_DrawerView    = null;
    private TextView	   m_MainTitle	 = null;
    private TextView	   m_SubTitle	 = null;
    private TextView	   m_User		 = null;
    private TextView	   m_BebnefitSend = null;
    private MFNetworkManager m_oNetworManager = null;
    private MFDataManager    m_oDataManager   = null;
    private MFSharedPreference msp = null;
    private Intent it;
    private Toast toastVote;
    private String m_EventIdx = new String();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_benefit_user);
		it = getIntent();
		m_EventIdx = it.getStringExtra("event_idx");
		msp = new MFSharedPreference(this);
		initialize();
        installEvent();
		setTitle("BenefitResult");

		//((ImageButton) findViewById(R.id.IB_BENEFIT_USER_HEADER_LEFT_BUTTON)).setOnClickListener(this);
		//List<ActionInfoTableVO> alsHistoryItems = DatabaseManager.getInstace().selectTableActionInfo();
		//setHistoryData(mBenefitItems);
		
		//setDumyData();

	}
	
	@Override
    public void onClick(View v)
    {
        MFDebug.d(">> onClick()");
        
        switch( v.getId() ) {
            case R.id.IB_BENEFIT_USER_HEADER_LEFT_BUTTON :
            	MFDebug.d("++ v.getId() : " + "IB_BENEFIT_HEADER_RIGHT_BUTTON");
				Intent it = new Intent(getApplicationContext(), BenefitDetailActivity.class);
				startActivityForResult(it,v.getId());
                break;
            case R.id.TV_BENEFIT_USER_SEND:
            	MFDebug.d("++ v.getId() : " + "TV_BENEFIT_USER_SEND");
    	        MFDataManager mem_idx = MFDataManager.getInstance(this);
    	        try{
    	        	//m_oNetworManager.requestBenfitVote(mem_idx.getMebmerIdx(),m_EventIdx,"1",this);
    	        	m_oNetworManager.requestBenfitVote("1","91","1",this);
    	        }catch (Exception e){
    	        	
    	        }
            	break;
            default :
                break;
        }
        
        super.onClick(v);
    }
	
    private void initialize()
    {
        MFDebug.d(">> initialize()");
        
        m_oNetworManager = MFNetworkManager.getInstance(this);
        m_oDataManager   = MFDataManager.getInstance(this);
        
        //하기에 멥머 정보 투입(null체크 필요) msp.getMem_idx()
        try{
        	//RequestServerBenefitUSerData(m_oDataManager.getMebmerIdx(), m_EventIdx);
        	RequestServerBenefitUSerData("1","75");
        } catch(Exception e){
        	
        }
        //Contents layout adding
        FrameLayout containerContents = (FrameLayout) findViewById(R.id.FL_BENEFIT_USER_CONTAINER);
        addContentsLayout(containerContents, R.layout.layout_benefit_user);
        
        //Drawer menu adding
        m_DrawerView = (FrameLayout) findViewById(R.id.FL_BENEFIT_USER_DRAWER_LAYOUT);
        addDrawerLayout(m_DrawerView);
        
        //set drawer layout (root)
        m_DrawerLayout = (DrawerLayout) findViewById(R.id.DL_BENEFIT_USER_NAVIGATION_LAYOUT);
    }
    
    private void installEvent()
    {
        MFDebug.d(">> installEvent()");
        
        m_MainTitle = (TextView) findViewById(R.id.TV_BENEFIT_USER_DESC1);
        m_SubTitle =  (TextView) findViewById(R.id.TV_BENEFIT_USER_DESC2);
        m_User =  (TextView) findViewById(R.id.TV_BENEFIT_USER_CNT);
        m_BebnefitSend =  (TextView) findViewById(R.id.TV_BENEFIT_USER_SEND);
        
        m_BebnefitSend.setOnClickListener(this);

       ((ImageButton) findViewById(R.id.IB_BENEFIT_USER_HEADER_LEFT_BUTTON)).setOnClickListener(this);
                
    }


	private void RequestServerBenefitUSerData(String mem_idx, String event_idx){
		 m_oNetworManager.requestBenfitUser(mem_idx, event_idx, this);	
	}
    
    
    @Override
	public void onNetMessage(int resultCode, NetMessage message) {
	    MFDebug.d(">> onNetMessage()");
	    MFDebug.d("++ resultCode : " + resultCode);
	    MFDebug.d("++ message : " + message);
	    
	    try {
	        //서버 여동 데이터
	        if( message instanceof NetJsonData ) {
	            long magicNum = ((NetJsonData) message).getMagicNum();
	            String json = ((NetJsonData) message).getResponseJson();
	            
	            if( json == null ) {
	                throw new Exception("invalid json data");
	            }
	            
	            Gson gson = new Gson();
	            
	            if( magicNum == MFNetworkManager.REQUEST_BENEFIT_USER ) {
	            	m_UserData = (BenefitUserData)gson.fromJson(json, BenefitUserData.class);
	                if( m_UserData == null ) {
	                    throw new Exception("Gson parsing error");
	                }
	                m_oDataManager.setBenefitUserData(m_UserData);
	                mBenefitUserData = (ArrayList<BenefitUserResponseData>) m_UserData.getData().clone();

	                m_MainTitle.setText(mBenefitUserData.get(0).getTitle());
	                m_SubTitle.setText(mBenefitUserData.get(0).getStart_date() + "~" + mBenefitUserData.get(0).getEnd_date());
	                m_User.setText(getString(R.string.benefit_cnt)+ " : " + mBenefitUserData.get(0).getMy_cnt() + " | " +
							getString(R.string.benefit_vote_cnt) + " : " + mBenefitUserData.get(0).getTot_mem_cnt());
	                m_BebnefitSend.setText(this.getString(R.string.benefit_send));
	                
	                
	                mBenefitUserItems = (ArrayList<BenefitUserResponseWinData>) m_UserData.getWin_data().clone();
	        		mLvBenefitResult = (ListView) findViewById(R.id.LV_BENEFIT_USER);
	        		String bkColor = new String("#FEF2DC");
	        		mLvBenefitResult.setBackgroundColor(Color.parseColor(bkColor));
	        				
	        		mBenefitUserAdaptor = new BenefitUserAdaptor(this, R.layout.list_item_benefit_user, mBenefitUserItems);
	        		mBenefitUserAdaptor.notifyDataSetChanged();
	        		mLvBenefitResult.setAdapter(mBenefitUserAdaptor);
	                
	            } else if( magicNum == MFNetworkManager.REQUEST_BENEFIT_VOTE ) {
                	mBenefitVoteDate = (BenefitVoteData)gson.fromJson(json, BenefitVoteData.class);
                    if( mBenefitVoteDate == null ) {
                        throw new Exception("Gson parsing error");
                    }
                    if( mBenefitVoteDate.getResultcode().equals("1") ){
                    	toastVote = Toast.makeText(this, getString(R.string.benefit_vote_success), Toast.LENGTH_LONG);
                    }else{
                    	toastVote = Toast.makeText(this, mBenefitVoteDate.getErrorcode() + ":" + getString(R.string.benefit_vote_fail), Toast.LENGTH_LONG);
                    }
                    toastVote.show();
                }
	        }
	        //이미지 데이터
	        else if( message instanceof NetImage ) {
	            
	        }
	        else {
	            //ignore...
	        }
	    } catch(Exception e) {
	        MFDebug.e(e);
	    }
	}


}